package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/22.<br><br>
 *
 * 打家劫舍
 */
public class Topic198 {

    public static void main(String[] args) {
        Topic198 instance = new Topic198();

        int[] array = new int[] {1,2,3,1};
        System.out.println(instance.rob(array));

        array = new int[] {2,7,9,3,1};
        System.out.println(instance.rob(array));

        array = new int[] {2,1,1,2};
        System.out.println(instance.rob(array));
    }

    public int rob(int[] nums) {
        if(nums.length == 0) {
            return 0;
        }

        if(nums.length == 1) {
            return nums[0];
        }

        if(nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int first = nums[0];
        int second = Math.max(first, nums[1]);
        for(int i = 2; i < nums.length; i++) {
            int temp = Math.max(first + nums[i], second);
            first = second;
            second = temp;
        }

        return second;
    }
}
